package com.ruoyi.blsj.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ruoyi.blsj.domain.BlsjBgsjzl;
import com.ruoyi.blsj.domain.BlsjYilanItem;
import com.ruoyi.blsj.service.IBlsjBgsjzlService;
import com.ruoyi.common.core.controller.BaseController;

import bp.tools.DateUtils;

/**
 * 报告事件资料Controller
 * 
 * @author ruoyi
 * @date 2023-10-09
 */
@Controller
@RequestMapping("/blsj/report")
public class BlsjReportController extends BaseController
{
    private String prefix = "blsj/report";

    @Autowired
    private IBlsjBgsjzlService blsjBgsjzlService;

    @RequiresPermissions("blsj:bgsjzl:yilanreport")
    @RequestMapping("/yilan")
    public String yilan(BlsjBgsjzl blsjBgsjzl, ModelMap mmap)
    {
    	if(blsjBgsjzl.getParams().get("tjlb")==null || "".equals(blsjBgsjzl.getParams().get("tjlb").toString())){
    		blsjBgsjzl.getParams().put("tjlb","1");
    	}
    	if(blsjBgsjzl.getParams().get("year")==null || "".equals(blsjBgsjzl.getParams().get("year").toString())){
    		blsjBgsjzl.getParams().put("year",DateUtils.getYear(new Date()));
    	}
    	if(blsjBgsjzl.getType()==null){
    		blsjBgsjzl.setType("");
    	}
    	mmap.put("blsjBgsjzl", blsjBgsjzl);
    	String title = "",headerName="";
    	if("1".equals(blsjBgsjzl.getParams().get("tjlb").toString())){
    		title = "("+blsjBgsjzl.getParams().get("year").toString()+"年度)上报科室分析统计报表";
    		headerName = "所属科室";
    	}else if("2".equals(blsjBgsjzl.getParams().get("tjlb").toString())){
    		title = "("+blsjBgsjzl.getParams().get("year").toString()+"年度)事件类型分析统计报表";
    		headerName = "事件类型";
    	}else if("3".equals(blsjBgsjzl.getParams().get("tjlb").toString())){
    		title = "("+blsjBgsjzl.getParams().get("year").toString()+"年度)事件等级分析统计报表";
    		headerName = "事件登记";
    	}else if("4".equals(blsjBgsjzl.getParams().get("tjlb").toString())){
    		title = "("+blsjBgsjzl.getParams().get("year").toString()+"年度)事件严重度分析统计报表";
    		headerName = "时间严重程度";
    	}else{
    		title = "("+blsjBgsjzl.getParams().get("year").toString()+"年度)上报科室分析统计报表";
    		headerName = "所属科室";
    	}
    	Map<String,BlsjYilanItem> tmpMap = new LinkedHashMap<String,BlsjYilanItem>();
//    	List<BlsjYilanItem> yiLanList = new ArrayList<BlsjYilanItem>();
    	List<Map<String,Object>> list = blsjBgsjzlService.selectTjlbReport(blsjBgsjzl);
    	List<String> listName = new ArrayList<String>();
    	for(Map<String,Object> mp: list){
    		if(tmpMap.containsKey(mp.get("name").toString())){
    			if("1".equals(mp.get("time").toString())){
    				tmpMap.get(mp.get("name").toString()).setJd1(Integer.parseInt(mp.get("count").toString()));
    			}else if("2".equals(mp.get("time").toString())){
    				tmpMap.get(mp.get("name").toString()).setJd2(Integer.parseInt(mp.get("count").toString()));
    			}else if("3".equals(mp.get("time").toString())){
    				tmpMap.get(mp.get("name").toString()).setJd3(Integer.parseInt(mp.get("count").toString()));
    			}else if("4".equals(mp.get("time").toString())){
    				BlsjYilanItem it = tmpMap.get(mp.get("name").toString());
    				System.out.println(it.getName());
    				tmpMap.get(mp.get("name").toString()).setJd4(Integer.parseInt(mp.get("count").toString()));
    			}
    			tmpMap.get(mp.get("name").toString()).setCount();
    		}else{
    			BlsjYilanItem blsjYilanItem = new BlsjYilanItem();
    			blsjYilanItem.setName(mp.get("name").toString());
    			listName.add(mp.get("name").toString());
    			if("1".equals(mp.get("time").toString())){
    				blsjYilanItem.setJd1(Integer.parseInt(mp.get("count").toString()));
    			}else if("2".equals(mp.get("time").toString())){
    				blsjYilanItem.setJd2(Integer.parseInt(mp.get("count").toString()));
    			}else if("3".equals(mp.get("time").toString())){
    				blsjYilanItem.setJd3(Integer.parseInt(mp.get("count").toString()));
    			}else if("4".equals(mp.get("time").toString())){
    				blsjYilanItem.setJd4(Integer.parseInt(mp.get("count").toString()));
    			}
    			blsjYilanItem.setCount();
    			tmpMap.put(mp.get("name").toString(), blsjYilanItem);
    		}
    	}
    	BlsjYilanItem zongji = new BlsjYilanItem();
    	zongji.setName("总计");
    	for (BlsjYilanItem tp : tmpMap.values()) {
    		zongji.setJd1(zongji.getJd1()+tp.getJd1());
    		zongji.setJd2(zongji.getJd2()+tp.getJd2());
    		zongji.setJd3(zongji.getJd3()+tp.getJd3());
    		zongji.setJd4(zongji.getJd4()+tp.getJd4());
    		zongji.setCount();
    	}
    	tmpMap.put("总计", zongji);
    	mmap.put("title", title);
    	mmap.put("headerName", headerName);
    	mmap.put("yiLanList", tmpMap.values());
    	mmap.put("listName", listName);
    	
        return prefix + "/yilanReport";
    }


}
